package com.yonyou.cyxdms.sysAuth.service.service.userorgsource.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonyou.cyxdms.sysAuth.service.entity.userorgsource.UserorgSourcePO;
import com.yonyou.cyxdms.sysAuth.service.mapper.userorgsource.UserorgSourceMapper;
import com.yonyou.cyxdms.sysAuth.service.service.userorgsource.UserorgSourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonyou.cyxdms.sysAuth.service.dto.userorgsource.UserorgSourceDTO;
import com.yonyou.cyx.function.exception.DALException;
import com.yonyou.cyx.function.utils.common.StringUtils;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangtan
 * @since 2020-07-07
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserorgSourceServiceImpl extends ServiceImpl<UserorgSourceMapper, UserorgSourcePO> implements UserorgSourceService {

	//日志对象
	private static final Logger logger = LoggerFactory.getLogger(UserorgSourceServiceImpl.class);

	@Autowired
	private UserorgSourceMapper userorgSourceMapper;

	/**
	 * 分页查询对应数据
	 *
	 * @param page 分页对象
	 * @param queryCondition 查询条件
	 * @return com.baomidou.mybatisplus.core.metadata.IPage<java.util.Map<java.lang.String,java.lang.Object>>
	 * @author zhangtan
	 * @since 2020-07-07
	 */
	@Override
	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true)
	public IPage<Map<String, Object>> selectPageBysql(Page page, Map<String, Object> queryCondition) {
		// 获取总条数
		page.setRecords(userorgSourceMapper.selectPageBySql(page, queryCondition));
		return page;
	}

	/**
	 * 根据查询条件返回结果集
	 *
	 * @param queryCondition 查询条件
	 * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
	 * @author zhangtan
	 * @since 2020-07-07
	 */
	@Override
	@Transactional(readOnly = true)
	public List<Map<String,Object >> selectListBySql(Map<String, Object> queryCondition) {
		return userorgSourceMapper.selectListBySql(queryCondition);
	}

	/**
	 * 根据查询条件返回结果集
	 *
	 * @param id 主键ID
	 * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
	 * @author zhangtan
	 * @since 2020-07-07
	 */
	@Override
	@Transactional(readOnly = true)
	public Map<String, Object> getById(Long id) {
		UserorgSourcePO userorgsourcepo = userorgSourceMapper.selectById(id);
		if(userorgsourcepo!=null){
			return userorgsourcepo.toMaps();
		}else{
			throw new DALException("查询失败,没有符合条件的数据");
		}
	}

	/**
	 * 根据DTO 进行数据新增
	 *
	 * @param userorgSourceDTO 页面DTO
	 * @return java.util.Map<java.lang.String,java.lang.Object>
	 * @author zhangtan
	 * @since 2020-07-07
	 */
	@Override
	public Map<String,Object> insert(UserorgSourceDTO userorgSourceDTO) {
		//对对象进行赋值操作
		UserorgSourcePO userorgsourcepo = userorgSourceDTO.transDtoToPo(UserorgSourcePO.class);
		//执行插入
		userorgSourceMapper.insert(userorgsourcepo);
		//返回插入的值
		return userorgSourceMapper.selectById(userorgsourcepo.getId()).toMaps();
	}

	/**
	 * 根据DTO 及ID 进行数据更新
	 *
	 * @param id 主键ID
	 * @param userorgSourceDTO 页面DTO
	 * @return java.util.Map<java.lang.String,java.lang.Object>
	 * @author zhangtan
	 * @since 2020-07-07
	 */
	@Override
	public Map<String,Object> update(Long id, UserorgSourceDTO userorgSourceDTO) {
		UserorgSourcePO userorgsourcepo = userorgSourceMapper.selectById(id);
		//对对象进行赋值操作
		userorgSourceDTO.transDtoToPo(userorgsourcepo);
		//执行更新
		userorgSourceMapper.updateById(userorgsourcepo);
		return userorgsourcepo.toMaps();
	}

	/**
	 * 根据ID 进行删除数据
	 *
	 * @param id
	 * @return void
	 * @author zhangtan
	 * @since 2020-07-07
	 */
	@Override
	public void deleteById(Long id) {
		int deleteCount = userorgSourceMapper.deleteById(id);
		logger.debug("删除数量：{}",deleteCount);
		if (deleteCount<=0) {
			throw new DALException("删除失败,没有符合条件的数据");
		}
	}

	/**
	 * 根据ids 进行删除
	 *
	 * @param ids
	 * @return void
	 * @author zhangtan
	 * @since 2020-07-07
	 */
	@Override
	public void deleteBatchIds(String ids) {
		List<Long> longList = StringUtils.convertStrToArray(ids,",", Long.class);
		int deleteCount = userorgSourceMapper.deleteBatchIds(longList);
		logger.debug("删除数量：{}",deleteCount);
		if (deleteCount!=longList.size()) {
			throw new DALException("删除失败,没有符合条件的数据");
		}
	}
}
